package com.hopu.mybatis01.controller;
import com.hopu.mybatis01.pojo.User;
import com.hopu.mybatis01.pojo.User1;
import com.hopu.mybatis01.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    private List<User> list = new ArrayList<User>();
    private long id = 1;
    @Resource
    private IUserService usermapper;
//查询
    @GetMapping
    public List<User> getUsers(String username,String password) {
        return usermapper.getUsers(username,password);

    }
    //添加
    @PostMapping
    public String add(@RequestBody User user){
        user.setId(id++);
        System.out.println(user);
        list.add(user);
        return "添加成功";
    }
    //删除
    @DeleteMapping
    public String remove(long id){
        list.removeIf(user -> user.getId() == id);//对每个User检查若他的id等于传入id则做删除操作
        return "删除成功";
    }
    //修改
    @PutMapping
    public String update(@RequestBody User user){
        for (User u : list) {
            if(u.getId() == user.getId()){
                u.setUsername(user.getUsername());
                u.setPassword(user.getPassword());
                return "修改成功";
            }
        }
        return "修改失败";
    }

}
